How to Get results from multiple tables using LINQ
How to Get results from multiple tables using LINQ
472
05-Sep-2023
Updated on 06-Sep-2023
Aryan Kumar
06-Sep-2023To retrieve results from multiple tables using LINQ, you can use the
Joinoperator. TheJoinoperator takes two or more tables as input and returns a new table that contains the rows from all the tables that match the join condition.The join operator has the following syntax:
where
sourceis the name of the first table,otherSourceis the name of his second table,column1is the name of the column in the first table that is used to join the tables, andcolumn2is the name of the columns of the second table that is used to join the tables.For example, the following code joins the
CustomersandOrderstables:This code returns a new table that containing the names of all the customers and the IDs of all orders.
You can also join three or more tables using the
Joinoperator. For example, the following code joins theCustomers,Orders, andProductstables:This code returns a new table containing the name of all customers, the IDs of all orders, and the names of all the products that they ordered.